home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 591 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Are Pure Functions always Virtual????
  5. Date: 5 Jan 1996 20:51:00 +1100
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4cisa4$g1n@werple.net.au>
  8. References: <4cilse$5li@news2.deltanet.com>
  9. NNTP-Posting-Host: werple.mira.net.au
  10.  
  11. olivas@deltanet.com (Sergio Olivas) writes:
  12.  
  13.  
  14. >I have a base class for accessing databases (BC++), the base class is
  15. >made of only pure virtual functions.  Two derived classes are written,
  16. >one for accessing databases through the Paradox Engine, the other
  17. >using the Borland Database Engine.
  18.  
  19. >Since I've heard that using virtual functions really eats into the 64k
  20. >automatic data segment, as well as adding overhead, is the 'virtual'
  21. >keyword really needed. -- and does it make any difference, assuming
  22. >I'm not going to further derive another class from the newly derived
  23. >class (in this case DB_BASE_PDX) ???
  24.  
  25. >eg..
  26. >  class DB_BASE {
  27. >{  ...
  28. >   virtual int NextRecord(int TableID) = 0;
  29. >  ..^^^^^ is this needed?
  30. > };
  31.  
  32. >The derived classes are something like
  33. >  class DB_BASE_PDX : public DB_BASE
  34. >  {
  35. >    ...
  36. >     int NextRecord(int TableID);
  37. >   ...
  38. >   }
  39.  
  40. The reason for having virtual functions, pure or not, is that you intend 
  41. to call the functions through a pointer or reference to the base class.
  42. For example:
  43.     void f(DB_BASE *p)
  44.     { p->NextRecord(1);
  45.     }
  46. This code will call the function defined in DB_BASE_PDX, or in whatever is
  47. the true class of object that 'p' points to. If you want this capability,
  48. then you need the pure virtual functions. However, if you only ever deal
  49. with the actual object, or a pointer or reference to the object's actual
  50. class, you don't need any virtual functions; you can just define
  51. NextRecord in each of the derived classes as ordinary functions and don't
  52. mention them at all in the base class.
  53.  
  54. The amount of memory consumed by the v-tables depends on how many virtual
  55. functions there are and on how many classes are in the hierarchy. In a
  56. typical implementation, in each non-abstract class (i.e., each class for
  57. which no pure virtual functions, in itself or inherited, remain to be
  58. implemented by real functions) one pointer is required for each virtual
  59. function in itself and its base classes. An abstract class, it seems to
  60. me, would not require a v-table. 
  61.  
  62. As far as the overhead is concerned (I presume you mean execution time 
  63. overhead), unless the code in your virtual functions is trivial, you are 
  64. unlikely to notice it.
  65.  
  66. Lastly, a pure function must be virtual; otherwise, it has no meaning and 
  67. no purpose.
  68.  
  69. David White
  70. davidw@werple.mira.net.au
  71.